package com.ask.javabagubooster.a_java_basis.quick;

import java.util.ArrayList;
import java.util.List;

public class Demo2 {
    private static int sum = 0; // 共享的可变状态

    public static void main(String[] args) throws InterruptedException {
        List<Integer> numbers = new ArrayList<>();
        for (int i = 1; i <= 1000; i++) {
            numbers.add(i);
        }

        // 并行流累加
        numbers.parallelStream().forEach(number -> {
            synchronized (Demo2.class) {
                sum += number;
            }
        });

        System.out.println("Expected sum: " + 500500);
        System.out.println("Actual sum: " + sum); // 结果可能不正确
    }
}
